package com.lqfan.autoAnswerQ.controller.wrongSubject;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqfan.autoAnswerQ.controller.importQuestions.PoiWordController;
import com.lqfan.autoAnswerQ.model.fileType.FileType;
import com.lqfan.autoAnswerQ.model.quesBank.QuesBankModel;
import com.lqfan.autoAnswerQ.model.quesBank.Question;
import com.lqfan.autoAnswerQ.model.wrongSubject.WrongSubject;
import com.lqfan.autoAnswerQ.service.fileType.IFileTypeService;
import com.lqfan.autoAnswerQ.service.wrongSubject.IWrongSubjectService;
import com.lqfan.autoAnswerQ.service.wrongSubject.impl.WrongSubjectServiceImpl;
import com.lqfan.autoAnswerQ.utils.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zml
 * @since 2020-04-25
 */
@RestController
@RequestMapping(value = "/wrongSubject",produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "错题")
public class WrongSubjectController {
    private static final String filePath = System.getProperty("user.dir");
    private static final Logger logger = LoggerFactory.getLogger(WrongSubjectController.class);

    @Autowired
    IWrongSubjectService wrongSubjectService;


    /**
     * 默认插入到默认路径
     * 插入和更新和删除同步
     * @param questionId
     * @param userId
     * @param subjectId
     * @param confirmNumber
     * @param isDeleteWrong
     * @return
     */
    @ApiOperation(value = "添加错题",notes = "添加错题")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "questionId",value = "题库Id",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "subjectId",value = "题目id",required = true, dataType = "String"),
                    @ApiImplicitParam(name = "confirmNumber",value = "作对次数记录",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "isDeleteWrong",value = "是否运行删除，是为0，不是为1",required = true, dataType = "String"),
                    @ApiImplicitParam(name = "path",value = "路劲 “/",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "name",value = "题库名称",required = true,dataType = "String")
            }
    )
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage  insertWrongSubject(@RequestParam("questionId") String questionId,
                                               @RequestParam("userId") String userId,
                                               @RequestParam("subjectId") String subjectId,
                                               @RequestParam("confirmNumber") String confirmNumber,
                                               @RequestParam("isDeleteWrong") Integer isDeleteWrong,
                                               @RequestParam("path") String path,
                                               @RequestParam("isError") String isError,
                                               @RequestParam("name") String name){
        logger.info("开始进行添加错题");
        int note =wrongSubjectService.insertWrongSubject(questionId,userId,subjectId,confirmNumber,isDeleteWrong,path,isError,name);
        switch (note){
            case 0:
                return ResponseMessage.fail("插入失败");
            case 1:
                return ResponseMessage.success("插入成功");
            case 2:
                return ResponseMessage.fail("更新错题失败");
            case 3:
                return ResponseMessage.fail("删除失败或被设置为不可删除");
            case 4:
                return ResponseMessage.success("达到错题训练效果，删除题目成功");
            case 5:
                return ResponseMessage.fail("未达标做对该题次数,题目继续保留");
            case 6:
                return ResponseMessage.fail("该题目不存在");
            case 7:
                return ResponseMessage.fail("设置为不可删除，无法进行更新操作");
            case 8:
                return ResponseMessage.success("答错初始化错题次数成功");
            case 9:
                return ResponseMessage.success("答错初始化错题次数失败");
            default:
                return ResponseMessage.fail("插入错题失败");

        }
    }


    @ApiOperation(value = "修改错题得配置信息",notes = "修改错题得配置信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "confirmNumber",value = "设定答题需要正确次数",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "isDeleteWrong",value = "是否运行删除错误",required = true, dataType = "Integer"),

            }
    )
    @RequestMapping(value = "updateSet",method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage  updateWrongSet(@RequestParam("confirmNumber") String confirmNumber,
                                               @RequestParam("userId") String userId,
                                               @RequestParam("isDeleteWrong") Integer isDeleteWrong) {
           int set =  wrongSubjectService.updateWrongSet(confirmNumber,userId,isDeleteWrong);
        switch (set){
            case 0:
                return ResponseMessage.fail("修改失败，暂无错题，不开放设置");
            case 1:
                return ResponseMessage.success("修改成功");
            default:
                return ResponseMessage.fail("修改失败");
    }
    }

    @ApiOperation(value = "删除错题题库",notes = "删除错题题库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String" ),
            @ApiImplicitParam(name = "questionId",value = "题库Id",required = true,dataType = "String" ),
    })
    @RequestMapping(value = "deleteQuestion",method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage  deleteWrongQuestion(@RequestParam("userId") String userId,
                                           @RequestParam("questionId") String questionId) {
                int result = wrongSubjectService.deleteWrongProduct(userId,questionId);
                switch (result) {
                    case 0:
                        return   ResponseMessage.fail("删除错误题库失败");
                    case 1:
                        return  ResponseMessage.success("删除错误题库成功");
                    default:
                        return   ResponseMessage.success("删除失败");
                }
    }


    @ApiOperation(value = "获取错题题库",notes = "获取错题题库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid",value = "错题父路劲 /",required = true,dataType = "String" ),
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String" ),
    })
    @RequestMapping(value = "getQuestion",method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage  getQuestion(@RequestParam("pid") String pid,
                                                @RequestParam("userId") String userId) {

        String src = filePath + "/source/"+3+"/"+ userId ;
        List<Question> result = wrongSubjectService.getWrongQuestion(pid,userId);

        if (result==null){
            return  ResponseMessage.fail("该路劲下暂无获取到对应得错题");
        }else {
            return  ResponseMessage.success(result);
        }
    }


    @ApiOperation(value = "错题分类路劲所以错题题库",notes = "错题分类路劲所以错题题库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String" ),
            @ApiImplicitParam(name = "path",value = "错题分类路劲",required = true,dataType = "String" ),
            @ApiImplicitParam(name = "fid",value = "文件id",required = true,dataType = "String" ),
    })
    @RequestMapping(value = "deleteWrongPath",method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage  deleteWrongPath(@RequestParam("userId") String userId,
                                            @RequestParam("path") String path,
                                            @RequestParam("fid") String fid) {
        int result = wrongSubjectService.deleteWrongPath(userId,path);
        switch (result) {
            case 0:
                return   ResponseMessage.fail("删除分类失败");
            case 1:
                return  ResponseMessage.success("删除错题分类成功");
            default:
                return   ResponseMessage.fail("删除分类失败");
        }
    }


    @ApiOperation(value = "获取题目相关信息", notes = "获取题目相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "quesBankId", value = "题库id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页的显示数量", required = true, dataType = "int")
    })
    @RequestMapping(value = "getWrongSubjectWithPage", method = RequestMethod.POST)
    public ResponseMessage getSubjectWithPage(@RequestParam(value = "userId") String userId,
                                              @RequestParam(value = "quesBankId") String quesBankId,
                                              @RequestParam(value = "currentPage") int currentPage,
                                              @RequestParam(value = "limit") int limit) {
        logger.info("获取题目信息");
        QuesBankModel quesBankModel = new QuesBankModel();
        //获取题库
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("questionId",quesBankId);
        WrongSubject wrongSubject = wrongSubjectService.getOne(queryWrapper);
        quesBankModel.setFileName(wrongSubject.getName());
        quesBankModel.setFilePath(wrongSubject.getPath());
        quesBankModel.setQuestionFileType(1);
        quesBankModel.setQid(wrongSubject.getQuestionId());
        quesBankModel.setUserId(wrongSubject.getUserId());
        QuesBankModel res = wrongSubjectService.getSubjectWithPage(quesBankModel, userId, quesBankId, currentPage, limit);
        return ResponseMessage.success(res);
    }

    @ApiOperation(value = "获取所有错题题目信息", notes = "获取所有错题题目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "selectType", value = "选择类型", required = true, dataType = "int"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页的显示数量", required = true, dataType = "int")
    })
    @RequestMapping(value = "getAllWrongSubjectWithPage", method = RequestMethod.POST)
    public ResponseMessage getAllSubjectWithPage(@RequestParam(value = "userId") String userId,
                                              @RequestParam(value = "selectType") int selectType,
                                              @RequestParam(value = "currentPage") int currentPage,
                                              @RequestParam(value = "limit") int limit) {
        logger.info("获取所有错题题目信息");
        QuesBankModel quesBankModel = new QuesBankModel();
        //获取题库
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        WrongSubject wrongSubject = wrongSubjectService.getOne(queryWrapper);
        String type ="";
        switch (selectType){
            case 0:
                type = "全部错题";
                break;
            case 1:
                type = "单选题";
                break;
            case 2:
                type = "多选题";
                break;
            case 3:
                type = "判断题";
                break;
            case 4:
                type = "填空题";
                break;
            default:
                break;
        }
        quesBankModel.setFileName(type);
        quesBankModel.setFilePath("/默认路劲");
        quesBankModel.setQuestionFileType(1);
        quesBankModel.setUserId(userId);
        String questionId = wrongSubject.getQuestionId();
        quesBankModel.setQid(questionId);
        QuesBankModel res = wrongSubjectService.getAllSubjectWithPage(quesBankModel, userId, questionId, currentPage, limit,selectType);
        if (wrongSubject==null){
            return ResponseMessage.fail("暂无错题");
        }
        if (res==null){
            return ResponseMessage.fail("暂无错误的"+type);
        }else {
            return ResponseMessage.success(res);
        }
    }
}

